$(function () {
    initDatagrid();
    $("#queryButton").click(function () {
        //开始时间与结束时间的判断
        var beginTime = $("input[name=beginTime]").val();
        var endTime = $("input[name=endTime]").val();
        if(beginTime>endTime){
            if(!endTime){
                $.easyui.messager.show("结束时间不能为空");

            }else{
                $.easyui.messager.show("开始时间不能大于结束时间");
            }
            return;
        }
        $("#listTable").datagrid('load', $('#queryForm').serializeObject());
    })

});


// 表格内容初始化
function initDatagrid() {
    var url = $.util.getRequestJson('/json/consumeManage.orderList.json');

    $('#listTable').datagrid({
        url: url,
        idField: 'id',
        queryParams:{
            type:'1'
        },
        fit: true,
        pageSize: 20,
        pagination: true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns: [
            [
                {
                    field: 'orderNo',
                    title: '订单编号',
                    align: 'center',
                    width: 15
                },
                {
                    field: 'phone',
                    title: '客户手机号',
                    align: 'center',
                    width: 12,
                    formatter:function (value, row, index) {
                        return value.toString().substr(0,3)+"****"+value.toString().substr(7);
                    }
                },
                {
                    field: 'name',
                    title: '客户姓名',
                    align: 'center',
                    width: 12
                },
                {
                    field: 'expendScore',
                    title: '消费积分',
                    align: 'center',
                    width: 10
                },
                {
                    field: 'totalNum',
                    title: '消费数量',
                    align: 'center',
                    width: 10
                },
                {
                    field: 'expendTime',
                    title: '消费时间',
                    align: 'center',
                    width: 12
                },
                {
                    field: 'status',
                    title: '结算状态',
                    align: 'center',
                    width: 10,
                    formatter: function (value, row) {
                        if (row.status == 0) {
                            aEl = ' <span style="color:orange ">未兑换</span>';
                        } else if (row.status == 1) {
                            aEl = ' <span style="color: limegreen">已兑换</span>';
                        }
                        return $.string.format(aEl);
                    }
                }, {
                field: 'id',
                title: '操作',
                align: 'center',
                width: 18,
                formatter: function (value, row) {
                    if (row.status == 0) {
                        aEl = '<a href=javascript:exchangeProduct("{1}"); class="easyui-tooltip " title="兑换">兑换</a>';
                        aEl += ' &nbsp;&nbsp;| &nbsp;&nbsp;<a href=javascript:orderDetail("{0}"); class="easyui-tooltip " title="详情">详情</a>';
                        /* } else if (row.status == 1) {
                             aEl = '<a href=javascript:returnProduct("{0}"); class="easyui-tooltip " title="退货">退货</a>';
                             aEl += ' &nbsp;&nbsp;| &nbsp;&nbsp;<a href=javascript:orderDetail("{0}"); class="easyui-tooltip " title="详情">详情</a>';
                      */
                    } else {
                        aEl = '<a href=javascript:orderDetail("{0}"); class="easyui-tooltip " title="详情">详情</a>';
                    }
                    //格式化操作按钮
                    return $.string.format(aEl, row.id, row.orderNo);
                }
            }
            ]
        ]
    });
}

// 查看订单详情   订单id
function orderDetail(orderId) {

    var title = "详情";
    var url = $.util.getRootPath() + '/page/shopManager/order/orderDetail.html';
    url = orderId ? url + '?id=' + orderId : url;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 650,
        height: 400,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton: false,
        closeButtonText: '关闭'
    });
}

// 退货   订单id
function returnProduct(orderId) {
    var title = "退货信息";
    var url = $.util.getRootPath() + '/page/shopManager/order/returnProduct.html';
    url = orderId ? url + '?id=' + orderId : url;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 600,
        height: 560,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '确认',
        onSave: function () {
            subForm(dialog);
        }
    });
}

function subForm(dialog) {
    var productNum = $('#productNum').val();
    var returnNum = $('#returnNum').val();
    var returnDiscribe = $('#returnDiscribe').val();
    if (returnNum > productNum) {
        $.easyui.messager.show("退货数量不能大于兑换数量");
        return;
    }
    if (!returnDiscribe) {
        $.easyui.messager.show("请填写退货原因");
        return;
    }
    var url = $.util.getRootPath() + '/storeHandle/orderReturn';
    var form = parent.$('#marketingForm').form('submit', {
        url: url,
        onSubmit: function () {
            return parent.$('#returnProduct').form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}

// 兑换商品  参数为订单id
function exchangeProduct(orderNo) {
    $.messager.confirm('确认信息','确定要兑换吗？',function(r){
        if(r) {
            var url = $.util.getRequestJson('/json/storeHandle.gatherByOrder.json');
            $.util.post(url, {payCode: orderNo}, function (json) {
                $.easyui.messager.show(json.message);
                if (json.success) {
                    $('#listTable').datagrid('reload');
                }
            });
        }
    });
}